package com.gocoolmall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.gocoolmall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000118604065";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCYv2UBqksl6fyBSa/pCBPDXIuQUMGWgu8P0F0n3s9aavMC868FuZMhM692qQ0N44Ol5kKAe2V7Co6o0tnH7Zn3r5UKrtdOIVTse1qGeMc77pRgqttrwtAyAbztB7SbfEFWS/Cqnt03UxQ0wtlbi/Yt1IOKCQWYMMToJG0Q9zogIXoVmSTNS7Lfi/QphyctjIVJ3GSWbrUfo/fW0USl2CUntBGATQ4SmgCSOSXFZfuSA2HScsra8aESy0ZpzpwuPDXmIDc/nQmRGmxGXgWWejKaMqyIVqlfG/UdJGzUhWhFQmzq8zRUZg3OSRTibYgvFgTxOi1mgwxRzEOOiedlp+6pAgMBAAECggEAUlWonnMquq8bmp0boyLldELoP8wn7Xd6YS3RaPJjFrvbbdXom89iDV2M0Qtiq//dMImjgxoAo4gXSc0N8wSSH76XOgDwsX6qBiafryhfTGDXvhua1sVV7/6b1bB3m/AoT9nzSiUD3RR9Kct12jDha+bHWJpwV47CNNedqq14uWQ4mOnFt1+F1Hk737HprI092OEv53HK2jc/yFRkduACgLgcwzG6Db8kYizL8fE4hbtk1zwoWKL/KQMUNxlgjx0cxsjakdWNZlMYoPFxmXMJ36qKWON7ryVxHJ4ilERghFQsmBkSzB6Hi9gBtc3/PDGwc8H7suyjV5HTe4PnFF+UeQKBgQDyFr/IkAaLuLPlIayKz7okKlw6xtLkWOXFjyGQreSrP6BQUw6qNirB2VkshNn1QpP2WfuiQmU4q0jRoZkI6GSO7rtYL7V6W3aR9+4ekEuyfpm5u1BD9f5aC/jwsyidk2i5UMj/3E4jQYwyX36a3e4blCW3EYRE1a3O5NlzhuTqAwKBgQChhmPqEK3NZJ9UDo0w0iBHin/LmSEnfcgUvsOnp4IgX9yyf/DEmFIRiUFEpHFWr0vc6D9GPnYPaenucJk8Ts1C2HPzB39Twf4Cp/4NLeOwguArddyi0LjYM1YwU3W3DQKNYXQz4XdyHrSy/QeAJmhcHPx2/fzkpPfHe+EpRQ164wKBgQCePn6YhISy2kZFVZCqoARl1Ya6KPL7rpxrWhcvsmuSASIyv0gLvSKP2hovPpwWdLGxS7pvowT1WQlddktJhlcNE0JE79Kpq5XC7pSpR9sjTxv/S1C5S9g6Vi8gb4vyDMRctlDc5s3wv8z7tG5WnxEg1+MbWojiUeauIet+V8PUrwKBgFUg6w+1cqRJX9+BO9B7LiAGUowPTHXvyL+GOr/lfKNrYnmBDHfQf5BWclum/0TcZnMqxA5VZQuCmpPcLCaosW2FfrabQPP3zdPAjhiMEvA4ScUd/QaiUok/pTEbC3NKHAo/Tmr5bWZr4Hhk+ssDcwT5EeX0vpASkbRFCcoiZpRZAoGATruacPmW5oStpSoO6/3tqwVJ50+KHgcigF9M6N4twe/FXkAorqCB8xKYnuIRTEl1svnyBmHvhKoSoZ4d17xxmBJsWUhVkQbgLO6YM4WsSDVh8B9WqfImyzSIcKgENk4vBbnFNlj+XpsdnL3+VhQ9b9TiYpaEvC40FxGz7O92cOs=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmZ9xgI4YoWxoE4RxKfPLrME7+ZJzPRIZtdgobHf5BuR4tD7yT3twrs7WC7glpI0J8PT8ycgqV2qC1E3BWCLaa91JlY343c+VtW7IQWmVszfldJhqPbfIgnSTJrYv/pa0GdQJ55IvzuTPxXYn1YPn5N8PIQ7ll3P2NhytFoA/VLgdkgM+9xgMFIQzrv0pvVL/Li/Xrd5e+c8noaP8udIUQZ1CcfxWPTPikQhsYkC3iBkHd+XAi25MqkV+nX/Odf7wcaVforKuwYbVKaMChfDId8ipvmctGQmxQnwXl9Ki/yARZVjBhlCmpBJz34eBN3T/rSJEpRRhVW/RVbAMKsq64QIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="http://iqcco68uqw.cdhttp.cn/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url ="http://member.gocoolmall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    //TODO 配置参数
    private String  timeout ="30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
